﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;

public partial class ChartRender : System.Web.UI.Page
{
    public  string backimage = "Backgroudimage.PNG";
    //public string titleBack = "Titleback.PNG";
    public Chart CreateTempChart(string stringTitle, string areaTitle, Unit width, Unit height)
    {
        //create new instance
        Chart chart = new Chart();
        chart.ImageLocation = "~/TempImages/ChartPic_#SEQ(300,3)";
        chart.Width = width;
        chart.Height = height;
        chart.RenderType = RenderType.BinaryStreaming;
        chart.ImageType = ChartImageType.Png;
        chart.Palette = ChartColorPalette.BrightPastel;
        chart.BackImage = backimage;
        //add legen
      
        //set border skin
        chart.BorderSkin.SkinStyle = BorderSkinStyle.FrameTitle8;
        chart.BorderSkin.BackColor = Color.Black;
        chart.BorderStyle = BorderStyle.None;
        chart.BorderSkin.BackSecondaryColor = Color.DarkBlue;
        chart.BorderSkin.BorderWidth = 2;
        
        //add area
        chart.ChartAreas.Add("Top");
    
        // Set Title font
        chart.ChartAreas["Top"].AxisX.TitleFont = new Font("Times New Roman", 8, FontStyle.Bold);
        chart.ChartAreas["Top"].BackColor = Color.Transparent;
        chart.ChartAreas["Top"].BackSecondaryColor = Color.Transparent;
        // Set Title color
       // chart.ChartAreas[0].AxisX.LabelStyle.Enabled = false;
        System.Web.UI.DataVisualization.Charting.Title title = new Title(stringTitle);
        title.Font=new Font("Arial",14);
        title.Alignment=ContentAlignment.MiddleLeft;
        title.ForeColor = Color.White;
        chart.Titles.Add(title);
        //area title
        Title testTitle = new Title();
        testTitle.Text = areaTitle;
        testTitle.Font = new Font(FontFamily.GenericSansSerif, 10F, FontStyle.Bold);
        testTitle.IsDockedInsideChartArea = true;
        testTitle.Docking = Docking.Top;
        testTitle.TextOrientation = TextOrientation.Auto;
        chart.Titles.Add(testTitle);
        
    return chart;
    }

   private  Chart DrawChartByChartTypet(Unit w, Unit h,SeriesChartType chartType)
   {
       Chart chart = CreateTempChart("Pie chart", "Area title", w, h);
       chart.Series.Add("Price");
       Series series = chart.Series[0];
       series.ChartArea = "Top";
       series.ChartType = chartType;
       series.XValueMember = "Key";
  //     series.XValueType = ChartValueType.Int32;
       series.YValueMembers = "Value";
//       series.YValueType = ChartValueType.String;
       chart.DataSource = Data.CreatDataforPieChart();
       chart.DataBind();
       Legend legend = new Legend("");
       legend.IsTextAutoFit = true;
       legend.BorderWidth = 1;
       legend.BorderColor = Color.Black;
       legend.BackColor = Color.Transparent;
       legend.BackSecondaryColor = Color.Transparent;
       chart.Legends.Add(legend);
       return chart;
   }
   private Chart DrawBarChart(Unit w, Unit h)
   {
       Chart chart = CreateTempChart("Pie chart", "Area title", w, h);
       chart.Series.Add("Price");
       Series series = chart.Series[0];
    //   series.Color = Color.Black;
      // series.BackSecondaryColor = Color.White;
       series["DrawingStyle"] = "Glazed";
       series.ShadowColor = Color.LightCyan;
       series.ShadowOffset = 2;
       series.ChartArea = "Top";
       series.ChartType =  SeriesChartType.Column;
       series.XValueMember = "Key";
         series.XValueType = ChartValueType.String;
       series.YValueMembers = "Value";
       chart.DataSource = Data.CreatDataforPieChart();
       chart.DataBind();
    //   chart.ChartAreas[0].AxisX.
       //       series.YValueType = ChartValueType.String;
      // chart.Customize
       return chart;
   }
    private Chart DrawPieChart(Unit w, Unit h)
    {
        Chart chart = DrawChartByChartTypet(w, h, SeriesChartType.Pie);
        foreach (var point in chart.Series[0].Points)
        {
            point.LegendText = point.AxisLabel;
            point.Label = " ";
        }

        return chart;
    }
    public Chart DrawPieChaerExploded(Chart chart)
    {
 
     chart.Series[0].Label = "#PERCENT{P1}";
     foreach (var point in chart.Series[0].Points)
     {
         point.LegendText = point.AxisLabel;
         point.CustomProperties = "Exploded=true";
         point.Label = "#PERCENT{P1}";
     }
        return chart;
    }
    public Chart DrawPieChartValueInsite(Chart chart)
    {

        chart.Series[0].Label = "#PERCENT{P1}";
        foreach (var point in chart.Series[0].Points)
        {
            point.LegendText = point.AxisLabel;
          //  point.CustomProperties = "Exploded=true";
            point.Label = "#PERCENT{P1}";
        }
        return chart;
    }


    public  void RenderChart(Chart chart)
    {
        using (MemoryStream memStream = new MemoryStream())
        {
            chart.SaveImage(memStream, ChartImageFormat.Png);
            Response.BinaryWrite(memStream.ToArray());
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        string type = Request.QueryString["type"];
        string mode = Request.QueryString["mode"];
        Unit w = 400;
        Unit h = 300;
        if (!String.IsNullOrEmpty(Request.QueryString["w"]))
        {
            w = Unit.Parse(Request.QueryString["w"]);
        }
        if (!String.IsNullOrEmpty(Request.QueryString["w"]))
        {
            h = Unit.Parse(Request.QueryString["h"]);
        }
        Chart chart = new Chart();
        switch (type)
        {
            case "pie":
                chart = DrawPieChart(w, h);
                break;
            case "bar":
                chart = DrawBarChart(w, h);
                break;
        }
        switch (mode)
        {
            case "exploded":
                chart = DrawPieChaerExploded(chart);
                break;
            case "in":
                chart = DrawPieChartValueInsite(chart);
                break;
        }
     //Chart pieChart=  DrawPieChart(400,300);
        RenderChart(chart);

    }
}